<?php

namespace Modules\Shop\Api;

use App\Models\Site;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use PayService;
use Auth;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\InvalidCastException;
use LogicException;
use Modules\Shop\Entities\Order;

/**
 * 支付
 * @package Modules\Shop\Api
 */
class PayController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth:sanctum']);
    }

    /**
     * 订单支付
     * @param Request $request
     * @param Order $order
     * @return mixed
     * @throws BindingResolutionException
     * @throws InvalidCastException
     * @throws LogicException
     */
    public function pay(Request $request, Site $site, Order $order)
    {
        $type = request()->query('type'); //wepay
        $client = request()->query('client'); //miniapp app web
        //转换单位为分
        $order['total_fee'] = $order['price'] * 100;
        $order['out_trade_no'] = $order['sn'];
        $order['body'] = $order['subject'];
        $order['openid'] = $request->input('openid');
        return $this->$type($client, $order->toArray());
    }

    /**
     * 微信支付
     * @param mixed $client 支付方式如web/wap/miniapp
     * @param mixed $order
     * @return mixed
     * @throws BindingResolutionException
     */
    public function wepay($client, $order)
    {
        return PayService::wepay()->pay($client, $order);
    }
}
